package com.tsky.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import com.tsky.util.DataSource;

public class TraceAddressInfo {

	private static Connection conn = null;
	private Statement stmt = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private DataSource ds = null;
	
	static{
		try {
			if(conn == null)
				conn = new DataSource().InitConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 加载RNC信息
	 * @return
	 */
	public Map<String,String> getTraceInfoMap(String calling,String sdate,String edate){
		Map<String,String> map = new HashMap<String, String>();
		String sql = "SELECT t.start_time,(t.jd||'-'||t.wd) addr FROM user_info_data t " +
					 "WHERE t.calling = ? "+
					 "AND t.start_time BETWEEN to_date(?,'yyyy-mm-dd hh24:mi:ss') "+
					 "AND to_date(?,'yyyy-mm-dd hh24:mi:ss') ORDER BY t.start_time ASC ";
		try {
			ds = new DataSource();
			if(conn == null)
				conn = ds.InitConn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, calling);
			pstmt.setString(2, sdate);
			pstmt.setString(3, edate);
			rs = pstmt.executeQuery();
			while(rs.next()){
				map.put(rs.getString(1),rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs != null){
					rs.close();
					rs = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			try {
				if(stmt != null){
					stmt.close();
					stmt = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			try {
				if(conn != null){
					conn.close();
					conn = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return map;
	}
	
	public static void main(String[] args) {
		TraceAddressInfo trace = new TraceAddressInfo();
		String calling = "13822123396";
		String sdate = "20131019 6:00:00";
		String edate = "20131020 5:59:59";
		Map<String,String> map = trace.getTraceInfoMap(calling, sdate, edate);
		System.out.println(map.size());
	}
}
